﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：宣帅 
    //
    // 文件名：OrderConfirmDetail.cs
    // 文件功能描述：编辑采购单明细信息
    // 创建标识：宣帅2009-05-13
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.OrderManage
{
    public partial class OrderConfirmDetail : BasePage
    {
        IBLL_Purchase ip = Factory.BLL_Purchase();
        protected OrderConfirmDetail()
        {
            this.ModuleNo = Module.订单管理.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();
                SetAllBind();
            }
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetAllBind();
        }
        /// <summary>
        /// 绑定采购单信息
        /// </summary>
        private void SetBind()
        {
            string orderId = Request.QueryString["OrderID"];
            if (orderId == null)
            {
                //PageError("请选择采购单之后，再进入该页面", "");
                HZ.Web.WebUtil.Alert("请选择采购单之后，再进入该页面！", this);
                return;
            }

            int totalCount = 0;
            string companyID_PS = CurrentAccount.GetUser().Org.OrgID;
            Model.View.PurchaseOrder purchaseOrder = ip.GetPurchaseOrderByWhere(new HZ.DrugTrade.Model.Query.PurchaseOrder() { OrderID = orderId, SortFields = "PlanTime DESC" }, 1, 0, out totalCount)[0];
            //获取该采购单中本企业的产品总金额
            decimal totalAmount = ip.GetPurchaseOrderAmountOfCompany(orderId, companyID_PS);

            txtAmount.Text = totalAmount.ToString();
            txtHospital.Text = purchaseOrder.HospitalName;
            lblPurseOrder.Text = purchaseOrder.OrderName;
            lblStartTime.Text = purchaseOrder.StartTime.Value.ToString("yyyy-MM-dd");
            //lblEndTime.Text = purchaseOrder.PlanEndTime.Value.ToString("yyyy-MM-dd");
            txtRemarks.Text = purchaseOrder.Remarks;
            //ViewState["orderID"] = orderId;
            //ViewState["hID"] = purchaseOrder.HospitalID;
        }

        /// <summary>
        /// 绑定所有符合条件的采购单明细
        /// </summary>
        private void SetAllBind()
        {
            string orderId = Request.QueryString["OrderID"];
            if (orderId == null)
            {
                //PageError("请选择采购单之后，再进入该页面", "");
                HZ.Web.WebUtil.Alert("请选择采购单之后，再进入该页面！", this);
                return;
            }
            HZ.DrugTrade.Model.Query.PurchaseOrderDetail aPurchaseOrderDetail = new HZ.DrugTrade.Model.Query.PurchaseOrderDetail();
            aPurchaseOrderDetail.OrderID = orderId;
            aPurchaseOrderDetail.GoodsName = txtGoodsName.Text.Trim();
            aPurchaseOrderDetail.Outlookc = txtoutlookc.Text.Trim();
          
            switch (ddlStatus.SelectedItem.Text)
            {

                case "未确认":
                    aPurchaseOrderDetail.OrderDetailState = 1;
                    break;
                case "已确认":
                    aPurchaseOrderDetail.OrderDetailState = 2;
                    break;
                case "拒绝配送":
                    aPurchaseOrderDetail.OrderDetailState = 3;
                    break;
                case "全部":
                    aPurchaseOrderDetail.Append = "( OrderDetailState <> '0')";
                    break;
            }

            aPurchaseOrderDetail.CompanyID_PS = CurrentAccount.GetUser().Org.OrgID;
            aPurchaseOrderDetail.SortFields = "ExecuteTime desc ,OrderName ASC,HospitalName ASC,GoodsName ASC";

            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalDetail;
            try
            {
                gvwPurchaseOrderDetail.PageSize = AspNetPager1.PageSize;
                gvwPurchaseOrderDetail.DataSource = ip.GetPurchaseOrderDetailByWhere(aPurchaseOrderDetail, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalDetail); ;
                gvwPurchaseOrderDetail.DataBind();
                AspNetPager1.RecordCount = totalDetail;
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            publicStuate(2);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            SetAllBind();
        }
        /// <summary>
        /// 拒绝配送
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnConfirm0_Click(object sender, EventArgs e)
        {
            publicStuate(3);
        }
        /// <summary>
        /// 公共方法
        /// </summary>
        /// <param name="nStasta">2 确认 3 拒绝配送</param>
        private void publicStuate(int nStasta)
        {
            List<PurchaseOrderDetail> myList = new List<PurchaseOrderDetail>();

            //获取所有选择的目录
            string ids = "";

            //遍历列表的所有行
            for (int _index = 0; _index < gvwPurchaseOrderDetail.Rows.Count; _index++)
            {
                CheckBox ckb = ((CheckBox)gvwPurchaseOrderDetail.Rows[_index].FindControl("checkbox3"));
                if (ckb.Checked)
                {
                    ids += this.gvwPurchaseOrderDetail.DataKeys[_index].Value.ToString() + ",";
                }
            }

            //if (Request.Form["checkbox1"] != null)
            if (!string.IsNullOrEmpty(ids))
            {

                //string[] strCheck1 = ids.Split(',');
                //for (int i = 0; i < strCheck1.Length; i++)
                //{
                //    string Reason = "";
                //    //拒绝配送配送
                //    if (nStasta != 2)
                //    {                       
                //        TextBox txtReason = (TextBox)gvwPurchaseOrderDetail.Rows[i].FindControl("txtReason");
                //        Reason = txtReason.Text.Trim();
                //        if (string.IsNullOrEmpty(Reason))
                //        {
                //            HZ.Web.WebUtil.Alert("请填写拒绝配送理由！", this);
                //            ((TextBox)gvwPurchaseOrderDetail.Rows[i].FindControl("txtReason")).Focus();
                //            return;
                //        }
                //    }
                //    PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
                //    purchaseOrderDetail.OrderDetailID = strCheck1[i].ToString();
                //    purchaseOrderDetail.Reason = Reason;
                //    myList.Add(purchaseOrderDetail);
                //}
                //遍历列表的所有行
                for (int _index = 0; _index < gvwPurchaseOrderDetail.Rows.Count; _index++)
                {
                    CheckBox ckb = ((CheckBox)gvwPurchaseOrderDetail.Rows[_index].FindControl("checkbox3"));
                    if (ckb.Checked)
                    {
                        string Reason = "";
                        //拒绝配送配送
                        if (nStasta != 2)
                        {
                            TextBox txtReason = (TextBox)gvwPurchaseOrderDetail.Rows[_index].FindControl("txtReason");
                            Reason = txtReason.Text.Trim();
                            if (string.IsNullOrEmpty(Reason))
                            {
                                HZ.Web.WebUtil.Alert("请填写拒绝配送理由！", this);
                                ((TextBox)gvwPurchaseOrderDetail.Rows[_index].FindControl("txtReason")).Focus();
                                return;
                            }
                        }
                        PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail();
                        purchaseOrderDetail.OrderDetailID = this.gvwPurchaseOrderDetail.DataKeys[_index].Value.ToString();
                        purchaseOrderDetail.Reason = Reason;
                        myList.Add(purchaseOrderDetail);
                    }
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("请至少选定一个采购明细！", this);
                return;
            }
            if (myList.Count > 0)
            {
                int flag = 0;
                if (nStasta == 2)
                {
                    flag = ip.ConfirmPurchaseOrderDetail_Batch(myList);
                }
                else
                {
                    flag = ip.RefuseConfirmPurchaseOrderDetail_Batch(myList);
                }
                if (flag == 1)
                {
                    PageInfo("所选产品已成功确认！", "OrderConfirmDetail.aspx?OrderID=" + Request.QueryString["OrderID"].ToString());
                }
                else
                {
                    //PageError("确认失败！", "");
                    HZ.Web.WebUtil.Alert("确认失败！", this);
                }
            }
        }
    }
}
